package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import pers.cj.pdos.cache.CacheData;
import pers.cj.pdos.cache.CacheRemove;
import pers.cj.pdos.cache.CacheRemoveMore;
import pers.cj.pdos.constant.CacheConstant;
import pers.cj.pdos.entity.*;
import pers.cj.pdos.mapper.*;
import pers.cj.pdos.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.UserVo;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    UserHouseMapper userHouseMapper;
    @Autowired
    UserHangMapper userHangMapper;
    @Autowired
    UserHangGoodsMapper userHangGoodsMapper;
    @Autowired
    UserLastRetailMapper userLastRetailMapper;
    @Autowired
    UserHangSnMapper userHangSnMapper;

    @Override
    public User getById(String id) {
        return userMapper.selectById(id);
    }

    @Override
    public User getByUsername(String username) {
        return userMapper.selectOne(new QueryWrapper<User>().eq("username",username));
    }

    @Override
    public User getAdminByCompany(Company company) {
        return userMapper.selectOne(new QueryWrapper<User>().eq("company_id",company.getId())
                .eq("username",company.getAccount()));
    }

    @Override
    public UserVo getVoById(String id) {
        return userMapper.getVoById(id);
    }


    @Override
    public UserVo getVoByUserName(String userName) {
        return userMapper.getVoByUserName(userName);
    }

    @Override
    public boolean check(String id) {
        return Utils.check(userMapper.check(id));
    }

    @Override
    public List<UserVo> listVoPage(Integer page, Integer limit, String companyId, String searchStoreId, String searchContent, Integer status) {
        return userMapper.listVo(Utils.genPage(page,limit),limit,companyId,searchStoreId,Utils.genLikeData(searchContent),status);
    }

    @Override
    public long count(String companyId, String searchStoreId, String searchContent, Integer status) {
        return userMapper.count(companyId,searchStoreId,Utils.genLikeData(searchContent),status);
    }

    @Override
    public void insert(User user, List<UserRole> userRoleList, List<UserHouse> userHouseList) {
        userMapper.insert(user);
        userRoleMapper.insertBatch(userRoleList);
        userHouseMapper.insertBatch(userHouseList);
    }

    @Override
    @CacheRemove(clazz = PermissionServiceImpl.class, methodName="findByUserId", param = "#user.id")
    public void update(User user, List<UserRole> userRoleList, List<UserHouse> userHouseList) {
        //更新用户
        userMapper.updateById(user);

        //删除用户角色表
        userRoleMapper.delete(new QueryWrapper<UserRole>().eq("company_id",user.getCompanyId()).eq("user_id",user.getId()));

        //删除用户仓库表
        userHouseMapper.delete(new QueryWrapper<UserHouse>().eq("company_id",user.getCompanyId()).eq("user_id",user.getId()));

        //批量新增
        if (!Utils.isEmpty(userRoleList)) {
            userRoleMapper.insertBatch(userRoleList);
        }
        if (!Utils.isEmpty(userHouseList)) {
            userHouseMapper.insertBatch(userHouseList);
        }

    }

    @Override
    public boolean checkUsername(String username) {
        return userMapper.selectCount(new QueryWrapper<User>().eq("username",username)) > 0;
    }

    @Override
    @CacheRemove(clazz = PermissionServiceImpl.class, methodName="findByUserId", param = "#user.id")
    public void remove(User user) {
        userHangMapper.delete(new QueryWrapper<UserHang>().eq("user_id",user.getId()));
        userHangGoodsMapper.delete(new QueryWrapper<UserHangGoods>().eq("user_id",user.getId()));
        userHangSnMapper.delete(new QueryWrapper<UserHangSn>().eq("user_id",user.getId()));
        userLastRetailMapper.delete(new QueryWrapper<UserLastRetail>().eq("user_id",user.getId()));

        userHouseMapper.delete(new QueryWrapper<UserHouse>().eq("company_id",user.getCompanyId()).eq("user_id",user.getId()));
        userRoleMapper.delete(new QueryWrapper<UserRole>().eq("company_id",user.getCompanyId()).eq("user_id",user.getId()));
        userMapper.deleteById(user.getId());
    }

    @Override
    public boolean exist(String id) {
        if (Utils.isEmpty(id)) {
            return false;
        }
        return userMapper.selectById(id) != null;
    }
}
